<template>
  <div class="website-data">
    <el-card :body-style="{ padding: '0px' }">
      <div slot="header">
        <tools-btn
          :toolsData="toolsData"
          @addData="addData"
          @refresh="refresh"
          @search="search"
          @fieldSet="fieldSet"
          @hasOperation="hasOperation"
        />
      </div>
      <!-- card body -->
      <table-list
        id="tableExcel"
        :table="table"
        ref="tableList"
        @handleEdit="handleEdit"
        @handleDel="handleDel"
      >
        <template
          slot-scope="props"
          slot="maintainPlan"
        >
          <span v-text="formatTime(props.obj.row.maintainPlan)" />
        </template>
      </table-list>

      <pagination
        :page="page"
        @pageSizeChange="pageSizeChange"
        @pageCurrentChange="pageCurrentChange"
      />
    </el-card>
    <!-- 添加、修改弹出框 -->
    <el-dialog
      :title="editDialog.title"
      v-if="editDialog.visible"
      :visible.sync="editDialog.visible"
      @close="closeDialog('editDialog')"
      width="30%"
    >
      <el-form
        :model="editDialog.data"
        ref="editDialog.data"
        label-width="90px"
      >
        <el-form-item label="设备号">
          <el-input
            v-model="editDialog.data.imei"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="分配设备">
          <el-select v-model="sitetype" placeholder="请选择" size="mini"  @change="getValue">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护周期/天">
          <el-input
            v-model="maintainPlan"
            size="mini"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button
          size="small"
          plain
          @click="closeDialog('editDialog')"
        >取 消</el-button>
        <el-popover
          class="edit-dialog-popover"
          placement="top"
          v-model="editDialog.submitVisible"
        >
          <p style="text-align: center; margin-top: 0; color:#409eff">
            确定提交吗？
          </p>
          <div style="text-align: center">
            <el-button
              size="mini"
              @click="editDialog.submitVisible = false"
              v-text="'取 消'"
              plain
            />
            <el-button
              type="primary"
              size="mini"
              v-text="'确 定'"
              v-if="showPrise"
              @click="handleSubmit"
              plain
              id="btn"
            />
            <el-button
              type="primary"
              size="mini"
              v-text="'确 定'"
              v-if="showRentPrise"
              @click="handleAdd"
              plain
            />
          </div>
          <el-button
            slot="reference"
            type="primary"
            size="small"
            v-text="'提 交'"
            plain
          />
        </el-popover>
      </span>
    </el-dialog>
    <!-- 搜索弹出框 -->
    <el-dialog
      :title="searchDialog.title"
      v-if="searchDialog.visible"
      :visible.sync="searchDialog.visible"
      @close="closesearchDialog('searchDialog')"
      width="30%"
    >
      <el-form
        :model="searchDialog.data"
        ref="searchDialog.data"
        label-width="90px"
      >
       <el-form-item label="设备号">
          <el-input
            v-model="searchDialog.imei"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="设备地址">
          <el-input
            v-model="searchDialog.position"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="分配设备">
          <el-select v-model="searchDialog.rid" placeholder="请选择" size="mini"  @change="getValue">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button
          size="small"
          plain
          @click="closesearchDialog('searchDialog')"
        >取 消</el-button>
        <el-button
            slot="reference"
            type="primary"
            size="small"
            v-text="'确 定'"
            @click="searchSubmit"
            plain
          />
        <!-- <el-popover
          class="edit-dialog-popover"
          placement="top"
          v-model="searchDialog.submitVisible"
        >
          <p style="text-align: center; margin-top: 0; color:#409eff">
            确定提交吗？
          </p>
          <div style="text-align: center">
            <el-button
              size="mini"
              @click="searchDialog.submitVisible = false"
              v-text="'取 消'"
              plain
            />
            <el-button
              type="primary"
              size="mini"
              v-text="'确 定'"
              @click="searchSubmit"
              plain
              id="btn"
            />
          </div>
        </el-popover> -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableList from "../../components/Table/TableList";
import Pagination from "../../components/Pagination/pagination";
import ToolsBtn from "../../components/Tools/ToolsBtn";
import TableProp from "../../utils/tableProps.js";
import PageProp from "../../utils/pageProps2.js";
import HHead from "./WHead.js";
export default {
  name: "Website",
  data() {
    return {
      table: TableProp,
      page: PageProp,
      toolsData: {
        title: "设备管理",
        identifier: true,
        query: '',
        content: "请输入设备地址",
        oBtnShow: [
          { btn0: true }, // 新增按钮
          { btn1: true }, // 刷新按钮
          { btn2: true }, // 搜索按钮
          { btn3: false }, // 设置按钮
          { btn4: true } // 导出Excel按钮
        ]
      },
      editDialog: {
        title: "编 辑",
        visible: false,
        submitVisible: false,
        data: null
      },
      searchDialog: {
        title: "搜 索",
        visible: false,
        submitVisible: false,
        imei: '',
        position: '',
        rid: ''
      },
      maintainPlan: '',
      sitetype: '',
      showPrise:false,
      showRentPrise:false,
      query: '',
      options2: [],
      value2: '',
      Rid: ''
    };
  },
  components: {
    TableList,
    Pagination,
    ToolsBtn
  },
  created() {
    this.table.tHead = HHead;
    this.table.hasOperation = true;
    this.table.identifier = true;
    this.table.operation.data = [
      {
        id: 1,
        label: "编 辑",
        Fun: "handleEdit",
        btnType: "text",
        plain: false,
        size: "mini",
        className: "handleEdit"
      }
    ];
    this.getList(1, 10);
    this.equipment();
  },
  methods: {
    // 新增数据
    addData() {
      this.handleEdit();
    },
    // 列表数据刷新
    refresh() {
      this.searchDialog.imei = ''
      this.searchDialog.position = ''
      this.searchDialog.rid = ''
      this.getList(1, 10);
    },
    // 搜索
    search() {
      this.searchDialog.imei = ''
      this.searchDialog.position = ''
      this.searchDialog.rid = ''
      this.searchDialog.visible = true;
      this.searchDialog.submitVisible = true;
    },
    searchSubmit(){
      const params = {
        IMEI: this.searchDialog.imei == "" ? null:this.searchDialog.imei,
        address: this.searchDialog.position == "" ? null:this.searchDialog.position,
        rid: this.searchDialog.rid == "" ? null:this.searchDialog.rid
      }
      this.$api.WEBSITE.getData(params)
      .then(res => {
        const data = res.data;
        const total = res.count;
        this.table.data = data;
        this.page.total = total;
        this.searchDialog.visible = false;
        this.searchDialog.submitVisible = false;
      })
      .catch(() => {
        this.$message({
          message: "暂无数据",
          type: 'error'
        });
        this.table.data = []
        this.page.total = 0
        this.searchDialog.visible = false;
        this.searchDialog.submitVisible = false;
      });
    },
    // 字段设置
    fieldSet() {
    },
    // 隐藏表格操作列
    hasOperation() {
      this.table.hasOperation = !this.table.hasOperation;
    },
    // 获取数据列表
    getList(pageNo, pageSize) {
      this.table.data = [];
      this.table.loading = true;
      const params = {
        pageNo,
        pageSize
      };
      this.$api.WEBSITE.getData(params)
        .then(res => {
          const data = res.data;
          const total = res.count;
          this.table.data = data;
          this.page.total = total;
          this.table.loading = false;
        })
        .catch(() => {
          this.table.data = []
          this.page.total = 0
          this.$message({
            message: "暂无数据",
            type: 'error'
          });
        });
    },
    // 分配设备
    equipment(){
      this.$api.WEBSITE.equipmentData()
      .then(res => {
        var arr = []
        for(var i=0;i<res.data.length;i++){
          arr[i] = {};
          arr[i].label = res.data[i].person_name
          arr[i].value = res.data[i].rid
          if(res.data[i].rid == this.Rid){
            this.value2 = res.data[i].person_name
          }
        }
        this.options2 = arr;
      })
    },
    // 分页大小
    pageSizeChange(size) {
      const tableDataLength = this.table.data.length;
      this.page.pageSize = size;
      if (tableDataLength < this.page.pageSize) {
        this.getList(1, size);
      } else {
        const pageNo = this.page.currentPage;
        this.getList(pageNo, size);
      }
    },
    // 翻页
    pageCurrentChange(pageNo) {
      this.page.currentPage = pageNo;
      const size = this.page.pageSize;
      this.getList(pageNo, size);
    },
    // 编辑数据模态
    handleEdit(index, row) {
      this.editDialog.visible = true;
      if (index !== undefined && row !== undefined) {
        this.showPrise = true;
        this.showRentPrise = false; 
        const title = `${row.imei}编辑`;
        const data = row;
        const newDate = data.maintainPlan / (24 * 3600000);
        this.maintainPlan = newDate
        this.editDialog.title = title;
        this.editDialog.data = data;
        this.Rid = row.rid
        this.equipment();
      } else {
        this.showPrise = false;
        this.showRentPrise = true; 
        this.editDialog.title = "添加";
        this.editDialog.data = {};
      }
    },
    // 添加提交数据
    handleAdd(){
      this.editDialog.visible = false;
      this.editDialog.submitVisible = false;
      const params = {
        id: this.editDialog.data.id,
        IMEI: this.editDialog.data.imei,
        rid: this.sitetype,
        maintainPlan: this.maintainPlan * (24 * 3600000)
      }
      this.$api.WEBSITE.addData(params)
        .then(res => {
          this.getList(1, 10);
          this.$message({
            message: res.message,
            type: 'success'
          });
        })
        .catch(() => {
          this.$message({
            message: "添加失败",
            type: 'error'
          });
        });
    },
    // 编辑提交数据
    handleSubmit() {
      this.editDialog.visible = false;
      this.editDialog.submitVisible = false;
      const params = {
        id: this.editDialog.data.id,
        IMEI: this.editDialog.data.imei,
        rid: this.sitetype == '' ? this.editDialog.data.rid: this.sitetype,
        maintainPlan: this.maintainPlan * (24 * 3600000)
      }
     this.$api.WEBSITE.upData(params)
        .then(res => {
          this.getList(1, 10);
          this.$message({
            message: res.message,
            type: 'success'
          });
        })
        .catch(() => {
          this.$message({
            message: "修改失败",
            type: 'error'
          });
        });
    },
    // 删除
    handleDel() {
    },
    // 关闭模态
    closeDialog(name) {
      if (name === "editDialog") {
        this[name].submitVisible = false;
        this[name].data = null;
      }
      this[name].visible = false;
    },
    closesearchDialog(name){
      if(name === "searchDialog"){
        this[name].submitVisible = false;
        this[name].data = null;
      }
      this[name].visible = false;
    },
    // 时间转换
    formatTime(time) {
      const newDate = time / (24 * 3600000);
      return newDate;
    },
    // 编辑select
    getValue(value){
      this.sitetype = value
    }
  }
};
</script>

<style lang="scss" scoped>
.edit-dialog-popover {
  margin-left: 10px;
}
.typeSuccsee {
  color: #67c23a;
}
.typeError {
  color: #f56c6c;
}
.el-select{
  width: 100%;
}
/deep/ .el-card__header{
  position: relative;
  .el-input{
    width: 250px;
    position: absolute;
    top: 10px;
    left: 100px;
  }
}
/deep/ .el-dialog .el-dialog__title{
  font-size: 12px;
  
}
/deep/ .el-dialog .el-form-item__label{
    font-size: 12px;
  }
</style>
